﻿#include "addchannelform.h"
#include "ui_addchannelform.h"
#include "httpclient.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QMessageBox>
AddChannelForm::AddChannelForm(const QString &ip, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddChannelForm),
    m_ip(ip)
{
    ui->setupUi(this);
    this->setWindowTitle("添加通道信息");
}

AddChannelForm::~AddChannelForm()
{
    delete ui;
}

void AddChannelForm::on_btn_sure_clicked()
{
    QJsonObject json;
    json.insert("dev_type", ui->cbx_dev_type->currentIndex());
    json.insert("ip", ui->led_ip->text());
    json.insert("username", ui->led_username->text());
    json.insert("password", ui->led_password->text());
    json.insert("url", ui->led_url->text());
    json.insert("voice_ip", ui->led_voice_ip->text());
    json.insert("enable", ui->cbx_enable->currentIndex());
    json.insert("preview", ui->cbx_preview->currentIndex());
    json.insert("memo_fir", ui->led_memo_fir->text());
    json.insert("memo_sec", ui->led_memo_sec->text());
    json.insert("memo_thi", ui->led_memo_thi->text());
    QJsonDocument jsondoc;
    jsondoc.setObject(json);
    QString request = QString(jsondoc.toJson(QJsonDocument::Compact));
    QString request_path = QString("http://%1:8800/api/config/channel").arg(m_ip);
    HttpClient(request_path).success([this](const QString & response)
    {

        if(QMessageBox::information(nullptr, tr("通知"), tr("添加成功"), QMessageBox::Ok, QMessageBox::NoButton))
        {
            emit sig_clear();
            this->close();
        }

    }).json(request).post();
}
